<template>
	<!--  报警明细 -->
	<div class="content">
		<div class="pageSearchView">
			<el-form :inline="true" :model="searchParam" class="demo-form-inline" label-width="auto" style="width: 100%">
				<el-row>
					<el-col :span="6">
						<el-form-item label="车间/产线">
							<el-tree-select v-model="searchParam.groupIds" :data="workRoomList" check-strictly clearable
															:props="{ children: 'sonList', label: 'orgName', value: 'id' }" style="width: 250px"
															@change="organizationIdChange" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="设备" prop="equipmentList">
							<el-select v-model="searchParam.deviceIds" placeholder="请选择设备" clearable filterable style="width: 240px">
								<el-option v-for="item in equipmentData" :key="item.id" :label="item.equipmentName" :value="item.id">
									<span style="float: left">{{ item.equipmentName }}</span>
									<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px;">
										{{ item.equipmentCode }}
									</span>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="时间" prop="equipmentList">
							<el-date-picker
								v-model="searchParam.times"
								type="datetimerange"
								range-separator="到"
								start-placeholder="开始时间"
								end-placeholder="结束时间"
								value-format="YYYY-MM-DD HH:mm:ss"
							/>
						</el-form-item>
					</el-col>

					<el-col :span="4">
						<el-form-item>
							<el-button type="primary" :icon="Search" @click="getList()">查询</el-button>
              <el-button :icon="Refresh" @click="resetSearch()">重置</el-button>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<div class="pageTableView">
			<div class="pageTableMenuView">
				<div class="pageTableTitle">列表信息</div>
			</div>
			<!-- 表格 -->
			<el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 320px)">
				<el-table-column label="序号" align="center" width="70" fixed>
					<template #default="scope"> {{ (pageSearch.pageNum - 1) * pageSearch.pageSize + scope.$index + 1 }}
					</template>
				</el-table-column>
				<el-table-column prop="factoryName" label="车间" align="center" min-width="200" fixed />
				<el-table-column prop="deviceNo" label="设备编码" align="center" min-width="200" />
				<el-table-column prop="deviceNo" label="机台号" align="center" min-width="200" show-overflow-tooltip />
				<el-table-column prop="deviceName" label="设备名称" align="center" min-width="200" show-overflow-tooltip />
				<el-table-column prop="duration" label="持续时长(秒)" align="center" min-width="200" show-overflow-tooltip />
				<el-table-column prop="startTime" label="开始时间" align="center" min-width="200" show-overflow-tooltip />
				<el-table-column prop="endTime" label="结束时间" align="center" min-width="200" />
				<el-table-column prop="alarmCode" label="报警代码" align="center" min-width="200" />
				<el-table-column prop="alarmMessage" label="报警描述" align="center" min-width="200" />
			</el-table>
			<div class="pagePagination">
				<el-pagination background v-model:current-page="pageSearch.pageNum" v-model:page-size="pageSearch.pageSize"
											 :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next"
											 :total="pageTotal"
											 @size-change="handleSizeChange" @current-change="handleCurrentChange" />
			</div>
		</div>
	</div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { ElMessage, ElForm } from 'element-plus';
import axiosInstance from '@/utils/http/api';
import { Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue';
import moment from 'moment';

onMounted(() => {
	getList();
	getOption();
});


const searchForm = ref<InstanceType<typeof ElForm>>();

const pageSearch = ref({
	pageNum: 1,
	pageSize: 10
});

const dicData = ref([
	{ label: '新建', value: 0 },
	{ label: '待排产', value: 1 },
	{ label: '已排产', value: 2 },
	{ label: '下发', value: 3 },
	{ label: '开工', value: 4 },
	{ label: '中断', value: 5 },
	{ label: '暂停', value: 6 },
	{ label: '完成', value: 7 }
]);


const pageTotal = ref(null); // 页面总数
const searchParam = ref({
	times: [
		moment().startOf('day').format('YYYY-MM-DD HH:mm:ss'), // 当天开始时间
		moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')    // 当天结束时间
	]
});

const paginatedData = ref([]);
const workRoomList = ref([]); // 车间
const equipmentData = ref([]);
const equipmentOptions = ref([]);

const organizationIdChange = (e) => {
	searchParam.value.deviceIds = null
	axiosInstance.post('/api/basedataEquipment/getPage', { organizationId: e, current: 1, pageSize: 9999 })
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			equipmentData.value = res.data.records;
		});
};

const formRules = ref({
	organizationId: [{ required: true, message: '请选择工厂/车间', trigger: 'change' }],
	equipmentIds: [{ required: true, message: '请选择设备', trigger: 'change' }]
});


/**
 * 获取车间列表
 */
const getOption = () => {
	axiosInstance.post('/api/basedataEquipment/getPage', {
		current: 1,
		pageSize: 50
	})
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			equipmentOptions.value = res.data.records;
		});
	axiosInstance.post('/api/basedataOrganization/getAll', {})
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			workRoomList.value = res.data;
		});
};

/**
 * 获取列表数据
 */
const getList = () => {
	if (!searchParam.value.times) {
		ElMessage.error('请选择时间');
		return false;
	}
	const params = {
		...searchParam.value,
		...pageSearch.value,
		...{
			startTime: searchParam.value.times[0],
			endTime: searchParam.value.times[1]
		}
	};
	delete params.times;
	axiosInstance.get('/api/navigation/dmsDevice/alarmDetail', {
		params
	})
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			paginatedData.value = res.data.records;
			pageTotal.value = res.data.total;
		});
};

/**
 * 分页大小变化时处理
 * @param e 新的分页大小
 */
const handleSizeChange = (e) => {
	pageSearch.value.pageSize = e;
	getList();
};

/**
 * 当前页变化时处理
 * @param e 新的当前页
 */
const handleCurrentChange = (e) => {
	pageSearch.value.pageNum = e;
  getList();
};

/**
 * 提交表单
 */
const submitForm = () => {
	searchForm.value.validate((valid) => {
		if (valid) {
			getList();
		} else {
			ElMessage.error('请检查表单输入');
			return false;
		}
	});
};

/**
 * 重置搜索条件
 */
const resetSearch = () => {
	pageSearch.value.pageNum = 1;
  searchParam.value = {}
  // getList();
};
</script>
<style scoped></style>